package com.jp.game;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Menu;

import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.jp.game.databinding.ActivityMainBinding;
import com.jp.game.utils.SPUtil;
import com.jp.game.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);
        DrawerLayout drawer = binding.drawerLayout;
//        NavigationView navigationView = binding.navView;
//        LinearLayout leftLay = binding.leftDrawer;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        mAppBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
                .setOpenableLayout(drawer)
                .build();
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

        initDrawerLayout();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    /**
     * 初始化抽屉组件
     */
    private void initDrawerLayout() {
        // 禁止手势滑动
        binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        binding.appBarMain.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(SPUtil.getString("netUrl"))) {
                    binding.formLayout.netUrlEt.setText(SPUtil.getString("netUrl"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("dateRefresh"))) {
                    binding.formLayout.dateRefreshEt.setText(SPUtil.getString("dateRefresh"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("shotOne"))) {
                    binding.formLayout.shotOneEt.setText(SPUtil.getString("shotOne"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("levelOne"))) {
                    binding.formLayout.levelOneEt.setText(SPUtil.getString("levelOne"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("levelTwo"))) {
                    binding.formLayout.levelTwoEt.setText(SPUtil.getString("levelTwo"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("shotTwo"))) {
                    binding.formLayout.shotTwoEt.setText(SPUtil.getString("shotTwo"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("shotThree"))) {
                    binding.formLayout.shotThreeEt.setText(SPUtil.getString("shotThree"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("levelThree"))) {
                    binding.formLayout.levelThreeEt.setText(SPUtil.getString("levelThree"));
                }
                if (!TextUtils.isEmpty(SPUtil.getString("levelFour"))) {
                    binding.formLayout.levelFourEt.setText(SPUtil.getString("levelFour"));
                }
                saveParams();
                binding.drawerLayout.openDrawer(GravityCompat.START, true);
            }
        });
        binding.formLayout.saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveParams();
                EventBus.getDefault().post("refresh_home");
                closeDrawer();
                ToastUtil.show(MainActivity.this, "保存成功");
            }
        });
    }

    private void saveParams() {
        SPUtil.putString("netUrl", binding.formLayout.netUrlEt.getText().toString());
        SPUtil.putString("dateRefresh", binding.formLayout.dateRefreshEt.getText().toString());
        SPUtil.putString("shotOne", binding.formLayout.shotOneEt.getText().toString());
        SPUtil.putString("levelOne", binding.formLayout.levelOneEt.getText().toString());
        SPUtil.putString("levelTwo", binding.formLayout.levelTwoEt.getText().toString());
        SPUtil.putString("shotTwo", binding.formLayout.shotTwoEt.getText().toString());
        SPUtil.putString("shotThree", binding.formLayout.shotThreeEt.getText().toString());
        SPUtil.putString("levelThree", binding.formLayout.levelThreeEt.getText().toString());
        SPUtil.putString("levelFour", binding.formLayout.levelFourEt.getText().toString());
    }

    /**
     * 关闭抽屉组件
     */
    private void closeDrawer() {
        binding.drawerLayout.closeDrawer(GravityCompat.START, true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}